<?php
  //Calculate team totals by game and player grand totals
  $totalByGame = array();
  $playersTotal = array();
  foreach ($games as $game_id => $game)
  {
    $totalByTeam = array();
    foreach ($modality['teams'] as $team_id => $team)
    {
      // Initialize totals
      $totalByTeam[$team_id] = 0;

      // Calculate team total
      foreach ($team['players'] as $player_id)
      {
        if (!array_key_exists($player_id, $playersTotal))
        {
          $playersTotal[$player_id] = 0;
        }
        if (array_key_exists($player_id, $game['points']))
        {
          $playersTotal[$player_id] += $game['points'][$player_id]['sum'];
          $totalByTeam[$team_id] += $game['points'][$player_id]['sum'];
        }
      }
    }
    $totalByGame[$game_id] = $totalByTeam;
  }

  // Calculate grand total and order by grand total
  $grandTotalOrder = array();
  foreach ($modality['teams'] as $team_id => $team)
  {
    $teamTotal = array();
    $teamTotal['team_id'] = $team_id;
    $teamTotal['total']   = 0;

    // Calculate grand total
    foreach ($totalByGame as $game_id => $totalByTeam)
    {
      $teamTotal['total'] += $totalByTeam[$team_id];
    }

    // Insert to grand total array ordered
    $position = 0;
    while ($position<count($grandTotalOrder) && 
           $grandTotalOrder[$position]['total'] > $teamTotal['total'])
    {
      $position++;
    }
    for ($i=count($grandTotalOrder); $i>$position; $i--)
    {
      $grandTotalOrder[$i] = $grandTotalOrder[$i-1];
    }
    $grandTotalOrder[$position] = $teamTotal;
  }
?>

<table cellspacing="0" class="classificationTable">
  <tr>
    <th>Posi&ccedil;&atilde;o</th>
    <th>Equipe</th>
    <th>Jogador</th>
<?php foreach ($games as $game): ?>
    <th><?php echo $game['place'] ?></th>
<?php endforeach; ?>
    <th>Total</th>
  </tr>

<?php foreach ($grandTotalOrder as $index => $grandTotal): ?>
  <?php $playersCount = count($modality['teams'][$grandTotal['team_id']]['players']) ?>
  <tr>
    <td class="classificationTablePosition"rowspan="<?php echo $playersCount+1 ?>">
  <?php echo $index+1 ?>
    </td>
    <td class="classificationTableTeam" rowspan="<?php echo $playersCount+1 ?>">
  <?php echo $modality['teams'][$grandTotal['team_id']]['name'] ?>
    </td>
    <td class="classificationTableGrandTotal"></td>
  <?php foreach ($games as $game_id => $game): ?>
    <td class="classificationTableGrandTotal"><?php echo $totalByGame[$game_id][$grandTotal['team_id']] ?></td>
  <?php endforeach; ?>
    <td class="classificationTableGrandTotal"><?php echo $grandTotal['total'] ?></td>
  </tr>

  <?php foreach ($modality['teams'][$grandTotal['team_id']]['players'] as $player_id): ?>
  <tr>
    <td class="classificationTableTeam"><?php echo $players[$player_id]['name'] ?></td>
    <?php foreach ($games as $game_id => $game): ?>
      <?php if (array_key_exists($player_id, $game['points'])) : ?>
    <td class="classificationTableGame"><?php echo $game['points'][$player_id]['sum'] ?></td>
      <?php else : ?>
    <td class="classificationTableGame">0</td>
      <?php endif; ?>
    <?php endforeach; ?>
    <td class="classificationTableTotal"><?php echo $playersTotal[$player_id] ?></td>
  </tr>
  <?php endforeach; ?>

<?php endforeach; ?>
</table>
